package com.key.controller.admin.analysisReport;

import com.alibaba.fastjson.JSONObject;
import com.key.common.R;
import com.key.common.dto.AssetsFindDTO;
import com.key.common.vo.AssetListVO;
import com.key.common.vo.AssetsVO;
import com.key.service.CleanAssetsService;
import io.swagger.annotations.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/*
*
 * @description:清理清单资产Controller
 * @author: jianjiawei
 * @date: 2023/2/18 15:54
 **/
@Api(value = "清理清单资产接口API",tags = "清理清单资产接口API")
@Controller
@RequestMapping("/cleanAssets")
public class CleanAssetsController {
    @Autowired
    CleanAssetsService caService;
    /*
    *
     * @description:查询清理清单资产列表
     * @author: jianjiawei
     * @date: 2023/2/18 15:57
     **/
    @ApiOperation(value = "查询清理清单资产列表",notes = "显示所有清理资产信息",httpMethod = "post", response = AssetListVO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页显示总数",paramType = "query",dataType = "int",required = true)
    })
    @ResponseBody
    @RequestMapping(value = "/findCleanAssetsList",method = RequestMethod.POST)
    public R findCleanAssetsList(@RequestParam(name = "currentPage") Integer currentPage,@RequestParam(name = "pageSize") Integer pageSize){
        return caService.findCleanAssetsList(currentPage, pageSize);
    }

    /*
    *
     * @description:根据高级搜索查询清理资产接口
     * @author: jianjiawei
     * @date: 2023/2/18 16:00
     **/
    @ApiOperation(value = "根据高级搜索查询到期资产接口",notes = "根据高级搜索查询所有到期资产列表",httpMethod = "post", response = AssetListVO.class)
    @RequestMapping(value = "/findEndAssetsListByAdv",method = RequestMethod.POST)
    @ResponseBody
    public R findEndAssetsListByAdv(@RequestBody @ApiParam(name = "AssetsFindDTO",value = "资产高级搜索的数据传输对象",required = true) AssetsFindDTO findDTO){

        return caService.findCleanAssetsListByAdv(findDTO);
    }

    @RequestMapping(value = "/exportExcel",method = RequestMethod.GET)
    @ApiOperation(value = "EXCEL导出",notes = "把清理资产信息导出为excel",httpMethod = "get")
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=401,message ="用户未授权"),
            @ApiResponse(code=403,message="认证失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HSSFWorkbook workBook = caService.getWorkBook();
        String userAgent = request.getHeader("User-Agent");

        //excel文件名
        String fileName = "清理资产表"+System.currentTimeMillis()+".xls";
        // 针对IE或者以IE为内核的浏览器：
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        } else {
            // 非IE浏览器的处理：
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        //响应到客户端
        try {
            OutputStream os = response.getOutputStream();
            response.setContentType("application/x-xls;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            workBook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
